package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.apache.bcel.ExceptionConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetMealDishMapper setMealDishMapper;
    @Autowired
    CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addWithDish(SetmealDTO setmealDTO) {
        //导入setmeal数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        //导入setmeal_dish数据
//        setmealDTO.getSetmealDishes().forEach(setmealDish -> {
//            setMealDishMapper.insert(setmealDish);//单条插入
//        });
        //获取套餐id
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null&&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            //保存套餐和菜品的关联关系
            setMealDishMapper.insertBanch(setmealDishes);
        }
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //获取套餐信息
        Page<SetmealVO> pages = setmealMapper.selectPage(setmealPageQueryDTO);
        //获取查询total数
        long total = pages.getTotal();
        List<SetmealVO> results = pages.getResult();

        return new PageResult(total,results);
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //若套餐起售中，则不能删除
        ids.forEach(id->{
            Integer status = setmealMapper.getByIdWithcategoryName(id).getStatus();
            if(status==1){
                throw new BaseException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //删除套餐
        setmealMapper.deleteByIds(ids);
        //删除setmeal_dish中关联信息
        setMealDishMapper.deleteBysetmealId(ids);
    }

    @Override
    public SetmealVO getByIdWithDish(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //导出套餐数据
        Setmeal setmeal = setmealMapper.getByIdWithcategoryName(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        //导出setmeal_dish关联数据
        List<SetmealDish> setmealDishes = setMealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改setmeal表信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除setmeal_dish表原有信息
        List<Long> id = Collections.singletonList(setmealDTO.getId());
        setMealDishMapper.deleteBysetmealId(id);
        //插入setmeal_dish表新信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            //添加setmeal_id项
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        setMealDishMapper.insertBanch(setmealDishes);
    }

    @Override
    public void startOrStop(Integer status,Long id) {
        if(status==1){
            //查询关联食物是否起售
            List<Integer> statuses = setMealDishMapper.getStatusBySetmealId(id);
            //关联食物为停售状态，无法起售
            statuses.forEach(item->{
                if (item==0){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        //修改状态
        log.info("修改状态{},{}",status,id);
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 查询套餐信息
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> setmeals = setmealMapper.list(setmeal);
        return setmeals;
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemBySetmealId(Long id) {
        List<DishItemVO> dishItemVOS = setMealDishMapper.getDishItem(id);
        return dishItemVOS;
    }
}
